/**
 * Created by hetanhong on 2014/9/2.
 */
(function () {
    var tongzhi = function (config) {
        this._config = config;
        this._type = this.getConfig("type"); //公文类别
        this._businessType = this.getConfig("businessType");
        this._title = this.getConfig("title");
        this._businessId = this.getConfig("businessId");
        this._id = this.getConfig("id"); //公文id
        this._businessId = this.getConfig("businessId");
        this._taskName = this.getConfig("businessTypeText") || "通知中心"; //流程名称
        this._taskId = this.getConfig("taskId"); //待办id
        this._userId = this.getConfig("userId");
        this._div = (this).getConfig("div");
        this._createCardIds = this.getConfig("createCardIds");
        this.width = window.innerWidth || 320;
        this.height = window.innerHeight || 480;
        this.requestUrl = ""; //按钮操作交互地址
        this._format = d3.time.format("%Y-%m-%d");
    };
    if (typeof (Clickmed) === "undefined") {
        Clickmed = {};
    }
    Clickmed.tongzhi = tongzhi;
    tongzhi.prototype.init = function () {
        var me = this;
        window.plugins.getIntent.show(["OA", "正在获取数据"]);
        getDicts(function (dat) {
            me.Dicts = dat;
            var url = serverURL + "cmcp/user/getCurrentUserInfo";
            PostCP(url, null, function (dat) {
                me.userDat = dat;
                var url = serverURL + "cmcp/dynawork/getDynaWorkIndex";
                var self = {
                    id: me._businessId
                };
                console.log(self);
                //   向后台请求数据
                PostCP(url, self, function (dat) {
                    if (!dat) {
                        Prompt("获取数据失败");
                        return;
                    }
                    me.dat = dat;
                    me.draw();
                }, function () {
                    Alert("获取数据失败")
                })
            }, function () {
            });

        })

    };
    tongzhi.prototype.draw = function () {
        var me = this;
        //      me.getCode(); modify -2014-12-30 隐藏编号

        var render = this._config.render || d3.select("body");
        var d1 = this._config.body || d3.select("body");
        var width = this.width;
        var scrol = d1.append("div").classed("scrol", true).style({
            width: this.width + "px",
            height: this.height + "px",
            position: "absolute",
            background: "#fff",
            left: "0px",
            "z-index": 99,
            top: "0px"
        }).attr("id", "scrol");
        var warp = scrol.append("div").classed("dt_warp", true).style({
            position: "relative"
        });
        me.showButton();
        var datong_header = warp.append("div").classed("datong_header", true);

        var go_back = datong_header.append("div").classed("go_back", true);
        go_back.html("<i><img src =../../common/page/images/go_back_ico.png width =20 height=auto></i>返回");
        go_back.on(touch_s, function () {
            $(this).css("background-color", "#005ebf");
        });
        go_back.on(touch_e, function () {
            $(this).css("background-color", "");
            if (typeof (GLJ) != "undefined" && GLJ) {
                window.closeView();
                return;
            }
            setTimeout(function () {
                d1.selectAll("#scrol").remove();
                Android.back = TempFunction.back;
            }, Transiacion);

        });
        function back1() {

            setTimeout(function () {
                d1.selectAll("#scrol").remove();
            }, Transiacion);
            Android.back = TempFunction.back;
        }

        // Android返回键动作
        Android.back = TempFunction.back1 = back1;
        datong_header.append("h1").html(me._taskName || "查看来文");
        me.Redraw();

    };
    tongzhi.prototype.showButton = function () {
        var me = this;
        var warp = d3.select("#scrol .dt_warp");
        var button = warp.append("div").classed("tj_button2", true).classed("qb_btn_bg", true);
        var tj_button = button.append("p").style({
            "text-align": "center"
        });
        var input = tj_button.append("input").classed("other", true)
            .attr("type", "button")
            .attr("value", "阅 毕").style({
                width: "36.1%"
            });
        input.on("click", function () {
            if (Flag)
                return;
            Flag = true;
            setTimeout(function () {
                Flag = false;
            }, 500);
            var url = serverURL + "cmcp/noticenter/setEndTodo"
            PostCP(url, me._id, function (dat) {
                if (!dat) {
                    Prompt("提交失败");
                    return
                }
                if (dat == "success") {
                    Prompt("提交成功");
                    me._div.remove();
                    iScrollDiv.refresh();
                    setTimeout(function () {
                        d3.selectAll("#scrol").remove();
                    }, Transiacion);
                    Android.back = TempFunction.back;
                }
            }, function () {
            });
        });

    }
    tongzhi.prototype.getCode = function () {
        var me = this;
        var url = serverURL + "cmcp/code/getCode";
        PostCP(url, "0018", function (dat) {
            if (!dat) {
                Prompt("获取编号失败")
            }
            console.log(dat);
            me._Code = dat;
        }, function () {
            console.log("error");
        });
    };
    tongzhi.prototype.Redraw = function () {
        var me = this;
        console.log(me);
        var createCardIds = me._createCardIds;
        if (me.userDat && me.userDat["orgCode"]) {
            me._orgCode = me.userDat["orgCode"];
        }

        //  createCardIds = createCardIds.split(",");
        console.log(me);
        var warp = d3.select("#scrol .dt_warp");
        var title = me.dat["title"] || "工作动态";
        var subitems = me.dat["subitems"] || null;
        console.log(subitems);
        var scrol_ = warp.append("div").classed("scrol_", true).style({
            width: "100%",
            height: height - 100 + "px",
            postion: "relative",
            overflow: "hidden"
        }).attr("id", "scrol_");
        var datong_con = scrol_.append("div").classed("datong_con datong_con_re", true);
        datong_con.append("div").classed("tongzhiTitle", true).style({
            "text-align": "center"
        }).html(title);
        var datong_con = datong_con.append("div").classed("phone_cotent", true);
        for (var name in subitems) {
            if (!subitems[name]) {
                continue;
            }
            console.log(name);
            var report_title = datong_con.append("div").classed("report_title", true).html(me.turnName(name));
            for (var con in subitems[name]) {
                if (!subitems[name][con] || subitems[name][con] == {}) {
                    continue;
                }
                var ul = datong_con.append("ul");
                var li = ul.append("li").classed("b_btm", true).style({
                    padding: "5px 0px"
                });
                var dl = li.append("dl");
                dl.append("dd")//.html(" <b style='border-bottom:2px solid #2875c3'>事项内容 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</b> </br>");
                dl.select("dd").append("div").html(subitems[name][con]["content"].replace(/\n/g, "<br>")).style({
                    "font-size": "15px",
                    "margin-top": "3px"
                });
                //   dl.append("dd").html(" 事项内容</br>" + subitems[name][con]["content"].replace(/\n/g,"<br>"));
                console.log(createCardIds, subitems[name][con]["id"])
                if (!createCardIds.length || createCardIds.indexOf(subitems[name][con]["id"]) == -1) {
                	var pishi = me.dat.pishiUserIds;
                	if(pishi!=null && pishi.split(",").indexOf(me.userDat.userId)>-1){
                		var button = li.append("p").style({
                            "text-align": "right"
                        }).append("button").html("批   示").style({
                            height: "30px",
                            color: "#ffe"
                        }).attr("fromList", "d").attr("id", subitems[name][con]["id"])
                            .attr("issue", me.dat["issue"]).attr("noticenterId", me._id).attr("orgCode", me._orgCode);
                        button.on("click", function () {
                            if (Flag)
                                return;
                            Flag = true;
                            var obj = {};
                            if (me._orgCode == "001") {
                                me._superType = "0001"
                            } else {
                                me._superType = "0002"
                            }
                            me.deleteButton = $(this);
                            me._fromList = $(this).attr("fromList");
                            obj["id"] = $(this).attr("id");
                            me._dynaworkId = $(this).attr("id");
                            me._issue = $(this).attr("issue");
                            me._newTtile = "批示事项通知"
                            //  obj["issue"] = $(this).attr("issue");
                            me._noticenterId = $(this).attr("noticenterId");
                            //    obj["orgCode"] = $(this).attr("orgCode");
                            var url = serverURL + "cmcp/dynawork/getDynaWork";
                            PostCP(url, obj, function (dat) {
                                if (!dat) {
                                    Prompt("获取数据失败");
                                    return;
                                }
                                me.leaderPishi(dat);
                            }, function () {
                                Prompt("获取数据失败");
                            });

                            setTimeout(function () {
                                Flag = false;
                            }, 1000);
                        });
                    }

                }


            }

        }
        pishiScrol = new iScroll("scrol_", {hScrollbar: false, vScrollbar: false});
        window.plugins.getIntent.hide();
    };
    tongzhi.prototype.leaderPishi = function (obj) {
        var me = this;
        me.createDate = new Date();
        me._content = obj["content"];
        console.log(obj);
        var select = d3.select("body").append("div").classed("select", true).style({
            width: "100%",
            height: "100%",
            background: "#fff",
            position: "absolute",
            left: "0px",
            top: "0px",
            "z-index": 9990,
            "over-follow": "hidden"
        });
        var dt_warp = select.append("div").classed("dt_warp", true);
        var head = dt_warp.append("div").classed("datong_header", true);
        head.append("h1").html(me._taskName);
        var back = head.append("div").classed("go_back", true);
        back.html("<i><img src =../../common/page/images/go_back_ico.png width =16 height=auto></i>返回");
        back.on(touch_s, function () {
            $(this).css("background-color", "#005ebf");
        });
        // 左上角返回按钮 动作
        back.on(touch_e, function () {
            $(this).css("background-color", "");

            setTimeout(function () {
                d3.selectAll(".select").remove();
                // Android返回键动作更改
                Android.back = TempFunction.back1;
            }, 10);
        });
        // Android 返回键动作
        function back2() {
            $(".comList").remove();
            $(".mark").remove();
            d3.selectAll(".select").remove();

            Android.back = TempFunction.back1;
        }

        Android.back = TempFunction.back2 = back2;
        var datong_con = dt_warp.append("div").classed("datong_con", true).attr("id", "datong_con").style({
            height: height - 100 + "px",
            "over-flow": "hidden"
        });
        //  var content = datong_con.append("div").classed("content", true);
        var content = datong_con.append("div").classed("phone_cotent", true);
        var ul = content.append("ul");
        var li = ul.append("li").classed("b_btm", true).style({
            width: "100%"
        });
        var table = li.append("table").attr("cellpadding", "0").attr('cellspacing', "0").style({
            width: "100%"
        });
        var tr = table.append("tr");

        tr.append("td").html("<div>期&nbsp;号</div>").style({
            width: "25%"
        });
        tr.append("td").style("width", "75%").html(me._issue).classed("extend", true);
        /*  var tr = table.append("tr");

         tr.append("td").html("<div>编&nbsp;号</div>").style({
         width: "25%"
         });
         tr.append("td").style("width", "75%").html(me._Code).classed("extend", true);*/
        // modify -2014-12-30 隐藏编号
        var tr = table.append("tr");

        tr.append("td").html("<div>创&nbsp;建&nbsp;日&nbsp;期</div>").style({
            width: "25%"
        });
        tr.append("td").style("width", "75%").html(me._format(new Date(me.createDate))).classed("extend", true);
        var tr = table.append("tr");

        tr.append("td").html("<div>内&nbsp;容&nbsp;摘&nbsp;要</div>").style({
            width: "25%"
        });
        tr.append("td").style("width", "75%").html(obj["content"].replace(/\n/g, "<br>").replace(/\s/g, "&nbsp")).classed("extend", true);
        var leadString = "";
        var tr = table.append("tr");

        tr.append("td").html("<div>领&nbsp;导&nbsp;意&nbsp;见</div>").style({
            width: "25%"
        });
        var td = tr.append("td").style("width", "75%");
        var button = td.append("button").html(leadString)
            .classed("extend", true).classed("Edit", true)
            .attr("name", "领导意见").attr("type", "button");
        button.on(touch_e, function () {
            var name = $(this).attr("name");
            var html = $(this).html();
            var tempButton = $(this);
            if (Flag)
                return;
            Flag = true;
            setTimeout(function () {
                Flag = false;
            }, 500);
            Edit(name, html, function (tempstr) {
                console.log(tempstr);
                tempButton.html(tempstr);
                me._newContent = tempstr;
                tempScrol.refresh();
            });

        });

        var tj_button = select.append("div").classed("tj_button2", true).classed("qb_btn_bg", true).style({
            bottom: "0px"
        });
        if (me._orgCode == "001") {
            var ensure = tj_button.append("input").classed("btn_agree", true).attr("type", "button")
                .attr("value", "信息科办理");
        } else {
            var ensure = tj_button.append("input").classed("btn_agree", true).attr("type", "button")
                .attr("value", "秘书室办理");
            /*  var tr = table.append("tr");
             tr.append("td").html("<div>选&nbsp;择&nbsp;秘&nbsp;书&nbsp;室</div>").style({
             width: "25%"
             });
             var td = tr.append("td").style("width", "75%").style("position", "relative");
             var selectM = $("<select></select>");
             selectM.append("<option value=" + ">" + "</option>");
             for (var i = 0; i < mishushi.length; i++) {
             selectM.append("<option value=" + mishushi[i]["roleCode"] + ">" + mishushi[i]["roleName"] + "</option>");
             }
             $(td.node()).append(selectM);*/

        }
        tempScrol = new iScroll("datong_con", {
            hScrollbar: false,
            vScrollbar: false

        });
        ensure.on("click", function () {
            if (Flag)
                return;
            Flag = true;
            setTimeout(function () {
                Flag = false;
            }, 500);
            if (!$(".Edit").html().length) {
                Prompt("请填写意见");
                 return;
            }
            if (me._orgCode == "001") {
                me._roleCode = "076"
                me.submit();

            }
            else {
                me._roleCode = null;
                me.selectMishushi();
                return;

                me._roleCode = $("select").val();
                me.submit();
            }

        });

    };
    tongzhi.prototype.selectMishushi = function () {
        var me = this;

        var select = d3.select("body").append("div").classed("selectMishushi", true).style({
            width: "100%",
            height: "100%",
            background: "#fff",
            position: "absolute",
            left: "0px",
            top: "0px",
            "z-index": 9999,
            "over-follow": "hidden"
        });
        var dt_warp = select.append("div").classed("dt_warp", true);
        var head = dt_warp.append("div").classed("datong_header", true);
        head.append("h1").html(me._taskName);
        var back = head.append("div").classed("go_back", true);
        back.html("<i><img src =../../common/page/images/go_back_ico.png width =16 height=auto></i>返回");
        back.on(touch_s, function () {
            $(this).css("background-color", "#005ebf");
        });
        // 左上角返回按钮 动作
        back.on(touch_e, function () {
            $(this).css("background-color", "");

            setTimeout(function () {
                d3.selectAll(".selectMishushi").remove();
                // Android返回键动作更改
                Android.back = TempFunction.back1;
            }, 10);
        });
        // Android 返回键动作
        function back3() {
            $(".comList").remove();
            $(".mark").remove();
            d3.selectAll(".selectMishushi").remove();

            Android.back = TempFunction.back2;
        }

        Android.back = TempFunction.back3 = back3;
        var datong_con = dt_warp.append("div").classed("datong_con", true).attr("id", "datong_con").style({
            height: height - 44 + "px",
            "over-flow": "hidden"
        });
        datong_con.append("div").classed("report_title", true).html("选择收件人").style({
            "text-align": "center"
        });
        var dist = 122;
        var list = datong_con.append("div").classed("list", true).style({
            width: "100%",
            height: (height - dist) + "px",
            "over-follow": "hidden",
            "margin": "0 auto"
        }).attr("id", "list");
        var table = list.append("div").style({
            display: "table",
            width: "100%"
        }).attr("cellpadding", "0").attr("cellspacing", "0");
        var tr1 = table.append("div").classed("sjr_name", true);
        var ul = tr1.append("ul");
        var menber = me.dat.mishushi
        for (var j = 0; j < menber.length; j++) {
            var li = ul.append("li")
                .attr("roleCode", menber[j]['roleCode']).attr("userName", menber[j]["roleName"]);
            li.html("<i class= choose_ico></i>" + menber[j]["roleName"]);
            li.on("click", function () {
                if (Flag)
                    return;

                Flag = true;
                setTimeout(function () {
                    Flag = false;
                }, 500);
                $("li").removeClass("sel1");
                $(this).addClass("sel1");
                me._roleCode = $(this).attr("roleCode")
                d3.selectAll("i").classed("choosed_ico", false).classed("choose_ico", true);
                d3.select(this).select("i").classed("choosed_ico", true).classed("choose_ico", false);
            })
        }
        var tj_button = datong_con.append("div").classed("tj_button2", true).classed("qb_btn_bg", true).style({
            bottom: "0px"
        });


        var cancel = tj_button.append("input").classed("btn_back", true).attr("type", "button")
            .attr("value", "取  消");
        cancel.on("click", function () {
            if (Flag)
                return;

            Flag = true;
            setTimeout(function () {
                Flag = false;
            }, 500);
            $(".selectMishushi").remove();
            Android.back = TempFunction.back2;
        });
        var ensure = tj_button.append("input").classed("btn_agree", true).attr("type", "button")
            .attr("value", "确  认");
        ensure.on("click", function () {
            if (Flag)
                return;

            Flag = true;
            setTimeout(function () {
                Flag = false;
            }, 500);
            if (!me._roleCode) {
                Prompt("请选择秘书室");
                return;
            }
            me.submit();
        });
    };
    tongzhi.prototype.submit = function () {
        var me = this;
        var url = serverURL + "cmcp/dynawork/doPhoneStartSupervises"
        var self = {
            issue: me._issue,
            ///   superCode: me._Code, // modify -2014-12-30 隐藏编号
            fromList: me._fromList,
            content: me._content,
            superType: me._superType,
            title: me._newTtile,
            noticenterId: me._noticenterId,
            id: me._dynaworkId,
            szOpinion: me._newContent,
            roleCode: me._roleCode
        }
        PostCP(url, self, function (dat) {
            if (!dat) {
                Prompt("提交失败");
                return;
            }
            Prompt("提交成功");
            d3.select(".select").remove();
            d3.select(".selectMishushi").remove();
            me.deleteButton.remove();
            Android.back = TempFunction.back1;
            pishiScrol.refresh();
        }, function () {
            Prompt("提交失败");
        });
    };
    tongzhi.prototype.timeSelect = function () {

    };
    tongzhi.prototype.turnName = function (name) {
        var me = this;
        var Dicts = me.Dicts["dynamic_category"] || [];
        for (var i = 0; i < Dicts.length; i++) {
            if (Dicts[i]["code"] == name)
                return Dicts[i]["value"];
        }
        return "";
    };
    tongzhi.prototype.getConfig = function (key) {
        if (this._config == null) {
            return null;
        } else {
            if (typeof (this._config[key]) != "undefined") {
                return this._config[key];
            } else {
                return null;
            }
        }
    };
})()